
//
//  PLRLobbyViewController.m
//  PLRLiveShow
//
//  Created by 赵小燕 on 2017/8/5.
//  Copyright © 2017年 xiaoyanzhao. All rights reserved.
//

#import "PLRLobbyViewController.h"
#import "PLRBroadcastRoomViewController.h"
#import "PlcPlayerViewController.h"
#import "PLRSingleCollectionViewCell.h"
#import "UIScreen+ScreenSize.h"
#import "PLRLobbyModel.h"

static NSString * const cellIdentifier = @"KTableViewCellIdentifier";

@interface PLRLobbyViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
@property (nonatomic, strong) PLRLobbyModel *lobbyModel;

@end

@implementation PLRLobbyViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        _lobbyModel = [[PLRLobbyModel alloc] init];
        _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        _indicatorView.frame = CGRectMake([UIScreen screenwidth]/2 - 50, [UIScreen screenHeight]/2 - 50, 100, 100);
        _indicatorView.color = [UIColor lightGrayColor];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.collectionView registerClass:[PLRSingleCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    
    self.navigationItem.titleView = ({
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"大厅";
        [titleLabel sizeToFit];
        titleLabel;
    });
    
    self.navigationItem.rightBarButtonItem = ({
        UIBarButtonItem *button = [[UIBarButtonItem alloc] init];
        button.title = @"more";
        button.target = self;
        button.action = @selector(_onPressedBeginBroadcastButton:);
        button;
    });
    
    self.navigationItem.leftBarButtonItem = ({
        UIBarButtonItem *button = [[UIBarButtonItem alloc] init];
        button.title = @"刷新";
        button.target = self;
        button.action = @selector(_onPressedRefreshLobbyListButton:);
        button;
    });
    
    [self.view addSubview:_collectionView];
    [self.view addSubview:_indicatorView];
    
    [self startRequestData];
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (void)startRequestData {
    [_indicatorView startAnimating];
    [_lobbyModel requestLobbyListWithComplete:^{
        [_indicatorView stopAnimating];
        [_collectionView reloadData];
    }];    
}

- (void)_onPressedRefreshLobbyListButton:(UIBarButtonItem *)button {
    button.enabled = NO;
    self.collectionView.hidden = YES;
    
    [_lobbyModel requestLobbyListWithComplete:^{
        button.enabled = YES;
        self.collectionView.hidden = NO;
        [_collectionView reloadData];
    }];
}

- (void)_onPressedBeginBroadcastButton:(id)sender {
    PLRBroadcastRoomViewController *vc = [[PLRBroadcastRoomViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    PlcPlayerViewController *vc = [[PlcPlayerViewController alloc] initWithRoomInfo:_lobbyModel.lobbyArray[indexPath.row]];
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) {
    
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 100;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PLRSingleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    if (_lobbyModel.imageArray != nil && _lobbyModel.imageArray.count > 0) {
        cell.imageView.image = _lobbyModel.imageArray[indexPath.row];
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(([UIScreen screenwidth] - 20)/2, 180);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

@end
